package com.edu.udistrital.indexing.analyzer.eswordnet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.lucene.analysis.synonym.SynonymMap;
import org.apache.lucene.util.CharsRef;

import com.edu.udistrital.analyzer.dao.AnalyzerDao;
import com.edu.udistrital.analyzer.dto.SynsetDto;

public class SynonymEsWordNet {
	
	private AnalyzerDao analizerDao;
	private SynonymMap.Builder synMapBuilderEsWordNet;
	private SynonymMap synMapEsWordNet;

	private static final String  RELATION_NEAR_SYNONYM= "near_synonym";
	private static final String RELATION_XPOS_NEAR_SYNONYM = "xpos_near_synonym";
	
	public SynonymEsWordNet() throws IOException {
		super();

		analizerDao = new AnalyzerDao();
//		List<SynsetDto> listSynsetDto = analizerDao.obtenerSynsets();
		
		List<SynsetDto> listSynsetDto = new ArrayList<SynsetDto>();
		
		Collection<String> listRelaciones = new ArrayList<String>();
		listRelaciones.add(RELATION_NEAR_SYNONYM);
		listRelaciones.add(RELATION_XPOS_NEAR_SYNONYM);
		listRelaciones.add("has_hyponym");
		
		synMapBuilderEsWordNet = new SynonymMap.Builder(true); 
		
		List<SynsetDto> listSinonimosSynset = null;
		
		for(SynsetDto synsetDto: listSynsetDto) {
			listSinonimosSynset = analizerDao.obtenerRelationsSynset(synsetDto, listRelaciones);
			
			if(!listSinonimosSynset.isEmpty()) {
				synsetDto.setListSinonimosSynset(listSinonimosSynset);
			}
		}
		
		for(SynsetDto synsetDto: listSynsetDto) { 
			if(synsetDto.getListSinonimosSynset() != null) {
				for(SynsetDto sinonimoSynsetDto: synsetDto.getListSinonimosSynset()) {
					synMapBuilderEsWordNet.add(new CharsRef(synsetDto.getTermino()), new CharsRef(sinonimoSynsetDto.getTermino()), true);
				}
			}	
		}
	   
	    synMapEsWordNet = synMapBuilderEsWordNet.build();
	}

	public SynonymMap getSynMapEsWordNet() {
		return synMapEsWordNet;
	}

	public void setSynMapEsWordNet(SynonymMap synMapEsWordNet) {
		this.synMapEsWordNet = synMapEsWordNet;
	}
	
}
